/**
 * 
 */
package com.pentalog.common.test.spring.test;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.pentalog.common.bo.MultiDBService;
import com.pentalog.stock.dao.StockDao;
import com.pentalog.stock.dao2.AppUserDao;
import com.pentalog.stock.model.AppUser;
import com.pentalog.stock.model.Stock;

/**
 * @author tnguyenmanh
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/config/BeanLocations-HSQLDB.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class HelloSpringTestWithJUnitHSQLDB extends
		AbstractTransactionalJUnit4SpringContextTests {

	@Autowired
	private MultiDBService multiDBService;

	@Autowired
	private StockDao stockDao;

	@Autowired
	private AppUserDao userDao;


	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		// one-time initialization code
		System.out.println("@BeforeClass - oneTimeSetUp");
		System.out.println("-------------------------------");
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		// one-time cleanup code
		System.out.println("-------------------------------");
		System.out.println("@AfterClass - oneTimeTearDown");
		
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		System.out.println("@Before - setUp");
	}
	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		System.out.println("@After - tearDown");
	}
	
	
	@Test
	public void testMe() {
		System.out.println("Hello from Spring test...");
	}

	@Test
	public void testForService() {
		Assert.assertNotNull(multiDBService);
		multiDBService.doSomeThingWithMultiDB();
	}

	@Test
	public void testForDao() {
		Assert.assertNotNull(stockDao);
		Stock stock = new Stock();
		stock.setStockCode("7668");
		stock.setStockName("HAIO");
		stockDao.save(stock);
		stock = stockDao.findByStockCode("7668");
		Assert.assertNotNull(stock);
	}

	@Test
	public void testDaoOnDB2() {
		Assert.assertNotNull(userDao);
		AppUser user;

		user = userDao.findByUserName("tnguyenmanh");

		if (user == null) {

			user = new AppUser(false, false, "Hanoi", "1234", false,
					"tnguyenmanh@pentalog.fr", "Tuong", "Nguyen Manh",
					"ohlala", "tnguyenmanh");
			userDao.save(user);
		}

		System.out.println("Size = "+userDao.getAllAppUsers().size());
		Assert.assertTrue(0<userDao.getAllAppUsers().size());
	}

}
